package net.pinnoocle.playservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import net.pinnoocle.playcommon.exception.ShopException;
import net.pinnoocle.playcommon.response.ErrorCode;
import net.pinnoocle.playcommon.utils.ConvertUtils;
import net.pinnoocle.playdao.dao.RoomDao;
import net.pinnoocle.playdao.dao.ShopDao;
import net.pinnoocle.playdao.entity.Room;
import net.pinnoocle.playdao.entity.Shop;
import net.pinnoocle.playdao.entity.page.PageModel;
import net.pinnoocle.playdao.entity.page.PageQuery;
import net.pinnoocle.playdao.entity.shopPc.room.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 房间表(Room)表服务实现类
 *
 * @author ye
 * @since 2021-10-21 09:46:08
 */
@Service("roomService")
@Slf4j
public class RoomServiceImpl extends ServiceImpl<RoomDao, Room> {

    @Autowired
    ShopDao shopDao;
    @Autowired
    UserServiceImpl userService;

    /**
     * 房间列表+头部信息
     * @param params
     * @return
     */
    public RoomListVo roomList(RoomListSearchParams params){
        String shopId = params.getShopId();
        if(StringUtils.isBlank(shopId)){
            shopId = userService.getGlobalShopId();
        }
        if(StringUtils.isBlank(shopId)){
            throw new ShopException(ErrorCode.PARAMS_GET_ERROR,"shopId获取失败");
        }
        params.setShopId(shopId);
        PageModel<RoomListPageVo> roomListPageVoPageModel = roomListWithPage(params);
        int totalRoom = this.baseMapper.getTotalRoom(shopId);
        int totalUsedRoom = this.baseMapper.getUsedTotalRoom(shopId);
        RoomListVo roomListVo = new RoomListVo();
        roomListVo.setTotal(totalRoom);
        roomListVo.setTotalUse(totalUsedRoom);
        roomListVo.setListPageVo(roomListPageVoPageModel);
        return roomListVo;
    }

    /**
     * 房间列表---分页
     * @param params
     * @return
     */
    public PageModel<RoomListPageVo> roomListWithPage(RoomListSearchParams params){
        Map<String,Object> pageMap = new HashMap<>();
        pageMap.put("page",String.valueOf(params.getPage()));
        pageMap.put("pageSize",String.valueOf(params.getPageSize()));

        if(StringUtils.isBlank(params.getShopId())){
            String shopId = userService.getGlobalShopId();
            params.setShopId(shopId);
        }
        Page<RoomListPageVo> list = this.baseMapper.roomListWithPage(
                new PageQuery<Room>(pageMap).getPage(),params);
        List<RoomListPageVo> records = list.getRecords();
        if (CollectionUtils.isEmpty(records)) {
            return new PageModel<>(list, new ArrayList<>());
        }
        return new PageModel<>(list, records);
    }

    /**
     * 空闲房间列表---无分页
     * @return
     */
    public List<RoomListPageVo> roomList(){
        String shopId = userService.getGlobalShopId();
        List<Room> list = this.list(new QueryWrapper<Room>()
                .lambda()
                .eq(Room::getShopId, shopId)
                .eq(Room::getIsUsed, 0));
        return ConvertUtils.sourceToTarget(list,RoomListPageVo.class);
    }

    /**
     * 房间详情
     * @param roomId
     * @return
     */
    public RoomDetailVo roomDetail(String roomId){
        Room room = this.getById(roomId);
        RoomDetailVo roomDetailVo = ConvertUtils.sourceToTarget(room, RoomDetailVo.class);
        //店铺名称
        Shop shop = shopDao.selectById(room.getShopId());
        roomDetailVo.setShopName(shop.getShopName());
        return roomDetailVo;
    }

    /**
     * 添加房间
     * @param params
     * @return
     */
    public boolean saveRoom(SaveRoomParams params){
        String shopId = userService.getGlobalShopId();
        try {
            Room room = ConvertUtils.sourceToTarget(params, Room.class);
            room.setShopId(shopId);
            room.setCreatedTime(new Date());
            return this.save(room);
        } catch (Exception e) {
            log.info("添加房间失败："+e.getMessage());
            return false;
        }
    }

    /**
     * 编辑房间
     * @param params
     * @return
     */
    public boolean updateRoom(SaveRoomParams params){
        try {
            Room room = ConvertUtils.sourceToTarget(params, Room.class);
            return this.updateById(room);
        } catch (Exception e) {
            log.info("房间编辑失败："+e.getMessage());
            return false;
        }
    }

    /**
     * 删除房间
     * @param roomId
     * @return
     */
    public boolean deleteRoom(String roomId){
        try {
            return this.removeById(roomId);
        } catch (Exception e) {
            log.info("房间删除失败："+e.getMessage());
            return false;
        }
    }
}